package com.coollang.smartbra.presenter;

import com.coollang.smartbra.activity.ShareActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.model.ActionDataModel;
import com.coollang.smartbra.model.BaseModel;

import java.util.HashMap;
import java.util.Map;

import io.realm.RealmResults;

/**
 * @Auther: Timmy
 * @Data: 2017/7/12
 * @Fuction:
 */
public class ShareActivityPresenter extends BasePresenter<BaseModel> {

    private ShareActivity mShareActivity;

    private String[] actionType = {"", "31", "32", "33", "34", "35", "41"};

    public ShareActivityPresenter(ShareActivity shareActivity) {
        super(BaseModel.class);
        mShareActivity = shareActivity;
    }

    // 判断上传的是哪种类型
    private ActionDataModel mActionDataModel;


    // 添加计数运动数据
    public void uploadData(ActionDataModel model) {
        mActionDataModel = model;
        setRequestInfo();
    }

    @Override
    public void setRequestInfo() {

        if (mActionDataModel.getType() != 6) {
            setBaseOkgo("action/add-number-action");
        } else {
            setBaseOkgo("action/add-timing-action");
        }
    }

    @Override
    protected Map<String, String> getData() {

        Map<String, String> map = new HashMap<>();

        map.put("username", MyApplication.mLogInDataBean.getUsername());
        int type = mActionDataModel.getType();

        map.put("type", actionType[type]);
        map.put("ave_heart_rate", mActionDataModel.getAve_heart_rate() + "");
        map.put("max_heart_rate", mActionDataModel.getMax_rate() + "");
        map.put("min_heart_rate", mActionDataModel.getMin_rate() + "");
        map.put("start_time", mActionDataModel.getStart_time() + "");
        map.put("end_time", mActionDataModel.getEnd_time() + "");

        if (type != 6) {
            // 计数
            map.put("amount", mActionDataModel.getNums() + "");
            long duration = mActionDataModel.getEnd_time() - mActionDataModel.getStart_time();
            map.put("sport_duration", duration + "");
        } else {
            // 计时
            map.put("sport_duration", mActionDataModel.getNums() / 1000 + "");
        }
        return map;
    }

    @Override
    protected void parsrData(BaseModel baseModel) {
        if ("200".equals(baseModel.getCode())) {

        }
    }

    public ActionDataModel queryNumberActionData() {
        RealmResults<ActionDataModel> results = mRealm.where(ActionDataModel.class).findAll();
        ActionDataModel model = results.get(results.size() - 1);
        return model;
    }
}
